﻿using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;

namespace Bunbyx.game
{
    public class WelcomeScreen : Screen
    {
        // --------------------------------------------------------------------
        //  Constants
        // --------------------------------------------------------------------

        private const string SCREEN_NAME = "Screens/start_menu";

        // --------------------------------------------------------------------
        //  Enumerations
        // --------------------------------------------------------------------

        private enum MenuSelectState
        {
            AliceStoryHL,
            ChooseLevelHl,
            OptionsHl,
            QuitHl
        }

        private enum ActualMenu
        {
            Global,
            ChooseLevel,
            Options
        }

        // --------------------------------------------------------------------
        //  Delegates
        // --------------------------------------------------------------------

        public delegate void StartGame(string level);
        public delegate void ExitGame();
        public delegate void setDoingAliceState(bool state);

        // --------------------------------------------------------------------
        //  Attributes
        // --------------------------------------------------------------------

        private Texture2D bgTexture;
        private StartGame startGameHandler;
        private ExitGame exitGameHandler;
        private setDoingAliceState doingAliceHandler;
        private SpriteFont font;
        private SpriteFont smallFont;
        private int selectedLevel = 0;
        private int selectedOption = 0;
        private string[] levelList;
        private KeyboardState previousKeyboardState;
        private SoundPlayer soundPlayer;
        private MenuSelectState menuState;
        private ActualMenu actualMenu;
        private Dictionary<string, long> scores;

        // --------------------------------------------------------------------
        //  Constructor
        // --------------------------------------------------------------------

        public WelcomeScreen(StartGame startGameHandler, ExitGame exitGameHandler, string[] levelList, SoundPlayer soundPlayer, setDoingAliceState doingAlice, Dictionary<string, long> scores)
        {
            this.doingAliceHandler = doingAlice;
            this.startGameHandler = startGameHandler;
            this.exitGameHandler = exitGameHandler;
            this.levelList = levelList;
            this.soundPlayer = soundPlayer;
            this.scores = scores;

            actualMenu = ActualMenu.Global;
            menuState = MenuSelectState.AliceStoryHL;
            previousKeyboardState = Keyboard.GetState();
        }

        // --------------------------------------------------------------------
        //  Public methods
        // --------------------------------------------------------------------

        public override void LoadContent(ContentManager content)
        {
            bgTexture = content.Load<Texture2D>(SCREEN_NAME);
            font = content.Load<SpriteFont>(Hud.FONT_NAME);
            smallFont = content.Load<SpriteFont>(Hud.SMALL_FONT_NAME);
        }

        public override void Update(GameTime gameTime)
        {
            KeyboardState keyboardState = Keyboard.GetState();
            switch (actualMenu)
            {
                case (ActualMenu.Global):
                    {
                        if (keyboardState.IsKeyDown(Keys.Down) && !previousKeyboardState.IsKeyDown(Keys.Down))
                        {
                            switch (menuState)
                            {
                                case (MenuSelectState.AliceStoryHL): { menuState = MenuSelectState.ChooseLevelHl; break; }
                                case (MenuSelectState.ChooseLevelHl): { menuState = MenuSelectState.OptionsHl; break; }
                                case (MenuSelectState.OptionsHl): { menuState = MenuSelectState.QuitHl; break; }
                            }

                        }
                        if (keyboardState.IsKeyDown(Keys.Up) && !previousKeyboardState.IsKeyDown(Keys.Up))
                        {
                            switch (menuState)
                            {
                                case (MenuSelectState.ChooseLevelHl): { menuState = MenuSelectState.AliceStoryHL; break; }
                                case (MenuSelectState.OptionsHl): { menuState = MenuSelectState.ChooseLevelHl; break; }
                                case (MenuSelectState.QuitHl): { menuState = MenuSelectState.OptionsHl; break; }
                            }
                        }
                        if (keyboardState.IsKeyDown(Keys.Enter) && !previousKeyboardState.IsKeyDown(Keys.Enter))
                        {
                            switch (menuState)
                            {
                                case (MenuSelectState.AliceStoryHL): { doingAliceHandler(true); startGameHandler(levelList[5]); break; }
                                case (MenuSelectState.ChooseLevelHl): { actualMenu = ActualMenu.ChooseLevel; break; }
                                case (MenuSelectState.OptionsHl): { actualMenu = ActualMenu.Options; break; }
                                case (MenuSelectState.QuitHl): { exitGameHandler(); break; }
                            }
                        }
                        break;
                    }
                case (ActualMenu.ChooseLevel):
                    {
                        if (keyboardState.IsKeyDown(Keys.Down) && !previousKeyboardState.IsKeyDown(Keys.Down))
                        {
                            if (selectedLevel < levelList.Length - 1) { selectedLevel++; }
                        }
                        if (keyboardState.IsKeyDown(Keys.Up) && !previousKeyboardState.IsKeyDown(Keys.Up))
                        {
                            if (selectedLevel > 0) { selectedLevel--; }
                        }
                        if (keyboardState.IsKeyDown(Keys.Enter) && !previousKeyboardState.IsKeyDown(Keys.Enter))
                        {
                            doingAliceHandler(false);
                            startGameHandler(levelList[selectedLevel]);
                        }
                        if (keyboardState.IsKeyDown(Keys.Escape) && !previousKeyboardState.IsKeyDown(Keys.Escape))
                        {
                            actualMenu = ActualMenu.Global;
                        }
                        break;
                    }
                case (ActualMenu.Options):
                    {
                        if (keyboardState.IsKeyDown(Keys.Down) && !previousKeyboardState.IsKeyDown(Keys.Down))
                        {
                            if (selectedOption < 1) { selectedOption++; }
                        }
                        if (keyboardState.IsKeyDown(Keys.Up) && !previousKeyboardState.IsKeyDown(Keys.Up))
                        {
                            if (selectedOption > 0) { selectedOption--; }
                        }
                        if (keyboardState.IsKeyDown(Keys.Right) && !previousKeyboardState.IsKeyDown(Keys.Right))
                        {
                            if (selectedOption == 0)
                            { if ((int)(soundPlayer.MUSIC_VOLUME * 100) < 100) { soundPlayer.MUSIC_VOLUME += 0.01f; } }
                            else if (selectedOption == 1)
                            { if ((int)(soundPlayer.EFFECTS_VOLUME * 100) < 100) { soundPlayer.EFFECTS_VOLUME += 0.01f; } }
                        }
                        if (keyboardState.IsKeyDown(Keys.Left) && !previousKeyboardState.IsKeyDown(Keys.Left))
                        {
                            if (selectedOption == 0)
                            { if ((int)(soundPlayer.MUSIC_VOLUME * 100) > 0) { soundPlayer.MUSIC_VOLUME -= 0.01f; } }
                            else if (selectedOption == 1)
                            { if ((int)(soundPlayer.EFFECTS_VOLUME * 100) > 0) { soundPlayer.EFFECTS_VOLUME -= 0.01f; } }
                        }
                        if (keyboardState.IsKeyDown(Keys.Escape) && !previousKeyboardState.IsKeyDown(Keys.Escape))
                        {
                            actualMenu = ActualMenu.Global;
                        }
                        break;
                    }
            }
            previousKeyboardState = keyboardState;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(bgTexture, Vector2.Zero, Color.White);
            switch (actualMenu)
            {
                case (ActualMenu.Global):
                    {
                        //spriteBatch.DrawString(font, " BunbyX Menu", new Vector2(160, 100), Color.Red);
                        spriteBatch.DrawString(font, " Alice Story", new Vector2(160, 180), Color.OrangeRed);
                        spriteBatch.DrawString(font, " Choose a level", new Vector2(160, 220), Color.OrangeRed);
                        spriteBatch.DrawString(font, " Options", new Vector2(160, 260), Color.OrangeRed);
                        spriteBatch.DrawString(font, " Quit", new Vector2(160, 300), Color.OrangeRed);

                        switch (menuState)
                        {
                            case (MenuSelectState.AliceStoryHL): { spriteBatch.DrawString(font, " Alice Story", new Vector2(160, 180), Color.Gold); break; }
                            case (MenuSelectState.ChooseLevelHl): { spriteBatch.DrawString(font, " Choose a level", new Vector2(160, 220), Color.Gold); break; }
                            case (MenuSelectState.OptionsHl): { spriteBatch.DrawString(font, " Options", new Vector2(160, 260), Color.Gold); break; }
                            case (MenuSelectState.QuitHl): { spriteBatch.DrawString(font, " Quit", new Vector2(160, 300), Color.Gold); break; }
                        }
                        break;
                    }
                case (ActualMenu.ChooseLevel):
                    {
                        //spriteBatch.DrawString(font, " BunbyX Levels", new Vector2(160, 100), Color.Red);
                        int i = 0;
                        foreach (string level in levelList)
                        {
                            Color color = (selectedLevel == i) ? Color.Gold : Color.OrangeRed;

                            spriteBatch.DrawString(font, level, new Vector2(160, 180 + (40 * i)), color);
                            spriteBatch.DrawString(font, "(" + scores[level] + ")", new Vector2(660, 180 + (40 * i)), color);

                            i++;
                        }
                        break;
                    }
                case (ActualMenu.Options):
                    {
                        //spriteBatch.DrawString(font, " BunbyX Options", new Vector2(160, 100), Color.Red);
                        spriteBatch.DrawString(font, "Music Volume :" + ((int)(soundPlayer.MUSIC_VOLUME * 100)).ToString(), new Vector2(160, 180), Color.OrangeRed);
                        spriteBatch.DrawString(font, "Effects Volume :" + ((int)(soundPlayer.EFFECTS_VOLUME * 100)).ToString(), new Vector2(160, 220), Color.OrangeRed);
                        if (selectedOption == 0)
                        {
                            spriteBatch.DrawString(font, "Music Volume :" + ((int)(soundPlayer.MUSIC_VOLUME * 100)).ToString(), new Vector2(160, 180), Color.Gold);
                        }
                        if (selectedOption == 1)
                        {
                            spriteBatch.DrawString(font, "Effects Volume :" + ((int)(soundPlayer.EFFECTS_VOLUME * 100)).ToString(), new Vector2(160, 220), Color.Gold);
                        }

                        break;
                    }
            }

        }
    }
}
